import { Store } from 'iflux2';
import webApi from './webApi';
import { fromJS } from 'immutable';

import { message } from 'antd';

import OrderAcrtor from './actor/order-actor';
import { OrderType } from './constant/action-type';

export default class AppStore extends Store {
    bindActor() {
        return [
            new OrderAcrtor
        ]
    }

    queryAfterSaleDetail = async (tid) => {
        try {
            const { afterSaleOptionInfos, orderInfo } = await webApi.queryAfterSaleDetail(tid);
            this.dispatch(OrderType.INIT_ORDER, fromJS(orderInfo));
            this.dispatch(OrderType.INIT_AFTER_OPTION, fromJS(afterSaleOptionInfos))
        } catch (err) {
            console.log(err);
        }
    }

    /**
     * 退货申请
     */
    applyAfterSale = (params) => {
        return webApi.applyAfterSale(params).then((result) => {
            message.success('申请成功', 3);
            return result;
        }).catch(error => {
            message.error(error.message || '申请失败');
            return Promise.reject(error)
        })
    }

}